package cn.jserv.ddns.activator;

import com.huawei.hilink.openapi.plugin.PluginData;
import com.huawei.hilink.openapi.plugin.PluginListener;
import com.huawei.hilink.util.Logger;
import com.huawei.hilink.util.LoggerFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import java.util.Map;


/**
 * MainActivator
 *
 * @author jserv
 */
public class MainActivator implements BundleActivator, PluginListener {

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        context = bundleContext;
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        context = null;
    }

    @Override
    public long getBundleId() {
        Bundle bundle = context.getBundle();
        if (null == bundle) {
            return 0;
        }
        return bundle.getBundleId();
    }

    @Override
    public void unInstall() {
        pluginDataHandle.delAllDatas();
    }

    @Override
    public String createUIURL(String uiUrl) {
        Map<String, String> config = pluginDataHandle.getDatas();
        logger.debug("token", config.get("token"));
        return uiUrl + "?token=" + config.get("token");
    }

    protected void setPluginData(PluginData pluginData) {
        this.pluginDataHandle = pluginData;
    }

    protected void unsetPluginData(PluginData pluginData) {
        this.pluginDataHandle = null;
    }

    private static BundleContext context = null;

    private PluginData pluginDataHandle = null;

    private static final Logger logger = LoggerFactory.getLogger(MainActivator.class);

}
